package com.mall.service.marketing.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsFlashPromotionSession;
import com.mall.model.vo.SmsFlashPromotionSessionDetail;
import com.mall.service.marketing.aop.MallLog;
import com.mall.service.marketing.service.SmsFlashPromotionSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flashSession")
public class SmsFlashPromotionSessionController {

    @Autowired
    private SmsFlashPromotionSessionService smsFlashPromotionSessionService;

    /**
     * 获取全部场次
     * @return
     */
    @MallLog
    @GetMapping("/list")
    public CommonResult list() {
        List<SmsFlashPromotionSession> list = smsFlashPromotionSessionService.list();
        return CommonResult.success(list);
    }

    /**
     * 添加场次
     * @param smsFlashPromotionSession
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody SmsFlashPromotionSession smsFlashPromotionSession){
        boolean save = smsFlashPromotionSessionService.save(smsFlashPromotionSession);
        if (save) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.success("添加失败");
    }

    /**
     * 删除场次
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Integer id){
        boolean removeById = smsFlashPromotionSessionService.removeById(id);
        if (removeById) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.success("删除失败");
    }

    /**
     * 修改场次
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id, @RequestBody SmsFlashPromotionSession smsFlashPromotionSession){
        boolean updateById = smsFlashPromotionSessionService.updateById(smsFlashPromotionSession);
        if (updateById) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.success("修改失败");
    }

    /**
     * 修改启用状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public CommonResult updateStatus(@PathVariable Integer id, Integer status){
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionService.getById(id);
        smsFlashPromotionSession.setStatus(status);
        boolean updateById = smsFlashPromotionSessionService.updateById(smsFlashPromotionSession);
        if (updateById) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.success("修改失败");
    }

    /**
     * 获取场次详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable Integer id){
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionService.getById(id);
        return CommonResult.success(smsFlashPromotionSession);
    }

    /**
     * 获取全部可选场次及其数量
     * @param flashPromotionId
     * @return
     */
    @GetMapping("/selectList")
    public CommonResult selectList(Integer flashPromotionId){
        List<SmsFlashPromotionSessionDetail> promotionSessionList = smsFlashPromotionSessionService.selectList(flashPromotionId);
        return CommonResult.success(promotionSessionList);
    }
}
